<?php
// +----------------------------------------------------------------------
// | INPHP
// | Copyright (c) https://inphp.cc All rights reserved.
// | 该文件源码由INPHP官方提供，使用协议以INPHP官方公告为准。
// +----------------------------------------------------------------------
// | 分类
// +----------------------------------------------------------------------
namespace app\cms\model;

use Inphp\Core\Db\PDO\Model;

class CategoryModel extends Model
{
    protected string $tableName = "cms_category";

    /**
     * 由父级ID同步数据
     * @param int $parentId
     */
    public static function syncFromParent(int $parentId)
    {
        $list = self::emptyQuery("c")
            ->join(self::tableName()." p", "c.parentId=p.id")
            ->where("c.parentId", $parentId)
            ->select([
                "c.id, c.siteId, c.parentId, c.parentsId, c.hasChild",
                "p.siteId as parentSiteId, p.parentsId as parentParentsId"
            ])->orderBy("c.weight", "asc")
            ->get();
        foreach ($list as $item) {
            //如果 siteId 或 parentsId 不一致，则直接同步修改
            $parentsId = !empty($item["parentParentsId"]) ? "{$parentId},{$item["parentParentsId"]}" : $parentId;
            if ($parentsId != $item["parentsId"] || $item["siteId"] != $item["parentSiteId"]) {
                //执行同步修改
                if (self::emptyQuery()
                    ->where("id", $item["id"])
                    ->update([
                        "siteId"        => !empty($item["parentSiteId"]) ? $item["parentSiteId"] : null,
                        "parentsId"     => $parentsId
                    ])) {
                    //同步文章的
                    @ArchiveModel::emptyQuery()
                        ->where("categoryId", $item["id"])
                        ->update([
                            "siteId"        => !empty($item["parentSiteId"]) ? $item["parentSiteId"] : null,
                            "categoriesId"  => join(",", [$item["id"], $parentsId])
                        ]);
                }
                //再修改下级
                if ($item["hasChild"] == 1) {
                    self::syncFromParent($item["id"]);
                }
            }
        }
    }

    /**
     * 是否有子分类
     * @param int|array $id
     * @param int $hasChild
     */
    public static function setHasChild(int|array $id, int $hasChild = 2) {
        if ($id <= 0) {
            return;
        }
        $idArray = is_array($id) ? $id : [$id];
        if ($hasChild == 2) {
            foreach ($idArray as $cid) {
                if ($cid <= 0) {
                    continue;
                }
                $child = self::emptyQuery()
                    ->where("parentId", $cid)
                    ->rows();
                self::emptyQuery()
                    ->where("id", $cid)
                    ->update([
                        "hasChild"  => $child > 0 ? 1 : 0
                    ]);
            }
        } else {
            self::emptyQuery()
                ->whereIn("id", $idArray)
                ->update([
                    "hasChild"  => $hasChild == 1 ? 1 : 0
                ]);
        }
    }
}